package com.md24housecall.fragment;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jjoe64.graphview.GraphView.GraphViewData;
import com.md24housecall.activity.R;
import com.md24housecall.adapter.BaseIHealthAdapter;
import com.md24housecall.adapter.BloodOxygenAdapter;
import com.md24housecall.model.IHealthDataList;
import com.md24housecall.util.GlobalConstant;
import com.md24housecall.util.MyLog;


public class BloodOxygenFragment extends BloodPressureFragment{
	
	@Override
	protected View initView(LayoutInflater inflater, ViewGroup container) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.blood_pressure_ihealth, container,false);
	}
	
	@Override
	protected void initParams() {
		// TODO Auto-generated method stub
		super.initParams();
		sc=constant.SC_SPO2;
		sv=constant.SV_SPO2;
		deviceType="spo2.json";
		chartTitle=getActivity().getString(R.string.title_blood_oxygen);
	}
	
	@Override
	protected void parseJson(JSONObject object) throws JSONException {
		// TODO Auto-generated method stub
		if(object.has("BODataList")){
			JSONArray jsonArray=object.getJSONArray("BODataList");
			lvIHealth.stopRefresh();
			if(jsonArray!=null && jsonArray.length()>0){
				clearData();
				for(int i=0;i<jsonArray.length();i++){
					JSONObject jsonObj=jsonArray.getJSONObject(i);
					IHealthDataList dataList=new IHealthDataList();
					dataList.setBloodOxygen(jsonObj.getInt("BO"));
					dataList.setHR(jsonObj.getInt("HR"));
					
					dataList.setLat(jsonObj.getLong("Lat"));
					dataList.setLong(jsonObj.getLong("Lon"));
					dataList.setLastChangeTime(jsonObj.getLong("LastChangeTime"));
					dataList.setDataId(jsonObj.getString("DataID"));
					dataList.setDataSource(jsonObj.getString("DataSource"));
					dataList.setDate(jsonObj.getLong("MDate"));
					dataList.setNote(jsonObj.getString("Note"));
					cacheManager.insertBloodOxygen(dataList);
				}
			}
		}
	}
	
	@Override
	protected List<IHealthDataList> selectData() {
		// TODO Auto-generated method stub
		List<IHealthDataList> list=cacheManager.selectBloodOxygen(start,limit);
		return list;
	}
	
	@Override
	protected BaseIHealthAdapter getIHealthAdapter() {
		// TODO Auto-generated method stub
		return new BloodOxygenAdapter(getActivity(), listIHealthData);
	}
	
	@Override
	protected int getTotalData() {
		// TODO Auto-generated method stub
		int count=cacheManager.getTotalBloodOxygen();
		return count;
	}
	
	@Override
	protected GraphViewData[] loadGraphViewData() {
		// TODO Auto-generated method stub
		txvTitle1.setText(getActivity().getString(R.string.title_spo2));
		txvTitle2.setText(getActivity().getString(R.string.title_bpm));
		GraphViewData[] data = new GraphViewData[mGraphLength];
//		for(int i=0;i<mGraphLength;i++){
//			data[i]=new GraphViewData(i, listIHealthData.get(i).getBloodOxygen());
//		}
		int index=0;
		for(int i=mGraphLength-1;i>=0;i--){
			data[index]=new GraphViewData(index, listIHealthData.get(i).getBloodOxygen());
			index++;
		}
		return data;
	}
	
	@Override
	protected void clearData() {
		// TODO Auto-generated method stub
		if(isRefresh){
			cacheManager.clearBloodOxygenData();
			if(listIHealthData!=null && listIHealthData.size()>0)
				listIHealthData.clear();
		}
	}
	
	@Override
	protected void refresh() {
		// TODO Auto-generated method stub
		MyLog.d(GlobalConstant.TAG, "onRefresh blood oxygen");
		if(listIHealthData!=null && listIHealthData.size()>0)
			listIHealthData.clear();
	}
}
